//上下午 时间设置 字符串  start
var morningPeriod='';
var afternoonPeriod='';
//上下午 时间设置 字符串  end

//上午 下午  切换选择  start
$('.base-message-input select[name="am"]').change(function(){
    var timeSelect=$('.base-message-input select[name="section"]');
    if($(this).val() === '1'){
        timeSelect.html(morningPeriod);
    }else{
        timeSelect.html(afternoonPeriod);
    }
});
//上午 下午  切换选择  end

//预约医生  切换选择   start
$('#addReservationForm select[name="doctor_id"]').change(function(){
    $('#reservationDateInput').val('');
    $('#addReservationForm select[name="room"]').html('<option value="">请选择</option>');
    $('#addReservationForm select[name="am"]').html('<option value="">请选择</option>');
    $('#addReservationForm select[name="section"]').html('<option value="">请选择</option>');
});
//预约医生  切换选择   end

//预约日期输入框 获得焦点请求能预约的日期   start
$('#reservationDateInput').focus(function(){
    var _this=$(this);
    var doctor_id=$('#addReservationForm select[name="doctor_id"]').val();
    var date=$('div.datepicker-days.clearfix thead tr.date-header th.datepicker-switch').html();
    var year=date.substring(0,4);
    var month=parseInt(date.substring(date.length-1,date.length-3));
    if(month<10){month='0'+month};
    date=year+'-'+month+'-01';
    var todayDay=$('div.datepicker-days.clearfix tbody td.today').attr('data-day');

    var today=year+'-'+month+'-01';

    var result={doctor_id:doctor_id,time:today};
    if((_this.attr('data-doctorId') != result.doctor_id) || (_this.attr('data-time') != result.time)){
        $('div.datepicker-days.clearfix tbody td').addClass('disabled');
        $.get(RESERVATIONCENTERURL.doctorManagement.doctor.planInMonthAfterNow,result,function(data){
            if(data.code === 10000){
                $.each(data.data,function(i,v){
                    console.log(today,i)
                    if(checkTwoDate(today,i)){
                        var day=parseInt(i.substring(i.length,i.length-2));
                        $('div.datepicker-days.clearfix tbody td[data-day="'+day+'"]').removeClass('disabled');
                    }
                });
                $('div.datepicker-days.clearfix tbody td.new').addClass('disabled');
                _this.attr('data-doctorId',result.doctor_id);
                _this.attr('data-time',result.time);
            }else if(data.code === 40100){
                aClick();
            }
        });
    }
});
//预约日期输入框 获得焦点请求能预约的日期   end

//预约日期输入框 选择日期失去焦点  获取当天的诊室和时间  start
$('#reservationDateInput').blur(function(){
    var _this=$(this);
    var doctor_id=$('#addReservationForm select[name="doctor_id"]').val();
    setTimeout(function(){
        var times=_this.val();
        if(times){
            $.get(RESERVATIONCENTERURL.doctorManagement.doctor.plan,{doctor_id:doctor_id,times:times},function(data){
                if(data.code == 10000){
                    $('#addReservationForm select[name="room"]').html('<option value="'+data.data[times].room+'">诊室'+ data.data[times].room+'</option>');
                    if(data.data[times].workload['0'] && data.data[times].workload['1']){
                        $('#addReservationForm select[name="am"]').html('<option value="1">上午</option><option value="0">下午</option>');
                    }else if(data.data[times].workload['0']){
                        $('#addReservationForm select[name="am"]').html('<option value="0">下午</option>');
                        morningPeriod='';
                    }else if(data.data[times].workload['1']){
                        $('#addReservationForm select[name="am"]').html('<option value="1">上午</option>');
                        afternoonPeriod='';
                    }
                    $.each(data.data[times].workload,function(i,v){
                        if(v){
                            var str='';
                            for(var j=0;j<v.length;j++){
                                str+='<option value="'+v[j].start+'-'+v[j].end+'">'+v[j].start+':00-'+v[j].end+':00</option>';
                            }
                            if(i == '0'){
                                afternoonPeriod=str;
                            }else if(i == '1'){
                                morningPeriod=str;
                            }
                        }
                    });
                    $('.base-message-input select[name="am"]').change();
                }else if(data.code === 40100){
                    aClick();
                }else{
                    alertModel('',data.message);
                }
            });
        }else{
            $('#addReservationForm select[name="room"]').html('<option value="">请选择</option>');
            $('#addReservationForm select[name="am"]').html('<option value="">请选择</option>');
            $('#addReservationForm select[name="section"]').html('<option value="">请选择</option>');
        }
        _this.removeAttr('data-doctorId');
        _this.removeAttr('data-time');
    },200);
});
//预约日期输入框 选择日期失去焦点  获取当天的诊室和时间  end

//保存和取消  start
$('#addReservationSave').click(function(){
    var result=$('#addReservationForm').serialize();
    if($('.base-message-input input[name="time"]').val()){
        ajaxPost(RESERVATIONCENTERURL.addReservation.store, result, function(data){
            if(data.code === 10000){
                $.confirm({
                    title:'温馨提示',
                    body: '预约成功！',
                    okBtn:'继续预约',
                    cancelBtn:'查看预约',
                    transition: true,
                    okHidden:function(){
                        $('#addReservationClose').click();
                    },
                    cancelHidden:function(){
                        window.location.href = $('#addReservationSave').attr('data-url');
                    }
                });
            }else if(data.code === 40100){
                aClick();
            }else{
                alertModel('','预约失败，请重试！');
            }
        }, function(){});
    }else{
        alertModel('','请选择预约时间！');
    }
});
$('#addReservationClose').click(function(){
    window.location.href = $(this).attr('data-url');
});
//保存和取消  end